package com.senken.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.deser.Deserializers.Base;
import com.senken.base.BaseController;
import com.senken.commons.entity.FidStateAndPage;
import com.senken.commons.shiro.ShiroUser;
import com.senken.entity.Declares;
import com.senken.service.IDeclaresService;
import com.senken.service.IHiduserService;
import com.senken.service.ITotalstateService;

@Controller
@RequestMapping("/dec")
public class DeclareController extends BaseController{

	@Autowired
	private IDeclaresService declaresService;
	@Autowired 
	private ITotalstateService totalstateService;
	@Autowired
	private IHiduserService hiduserservice;
	
	/**
	 * 查找所有维修记录
	 * @return
	 */
	@RequestMapping("/selectdeclare")
	@ResponseBody
	public List<Declares> selectdeclare(){
		return declaresService.selectdeclare();
				
	}
	/**
	 * 添加维修记录
	 */
	@RequestMapping("/adddeclare")
	@ResponseBody
	public Object adddeclare(String customer, String name, String phone, String device, String code, int num, String des,
			String way,int fid){
		int Hid;
		Hid=declaresService.addDeclares(customer, name, phone, device, code, num, des, way).getHid();
		if(Hid!=0){
			totalstateService.add(Hid, fid);
			return renderSuccess();
		}
		return renderError("添加失败");
	}
	/**
	 * 分页查询维修记录
	 */
	@RequestMapping("/selectPage")
	@ResponseBody
	public List<Declares> selectPage(int state,int fid,int page){
		return declaresService.selectdeclareByFid(fid, state, page);
	}
	/**
	 * 通用次数查询接口
	 */
	@RequestMapping("/counts")
	@ResponseBody
	public int selectCounts(int fid,int state){
		return declaresService.selectCountsByfidStateAndPage(fid, state);
	}
	
	/**
	 *  接单接口
	 */
	@RequestMapping("/taking")
	@ResponseBody
	public Object taking(int fid,int hid,int maintainer){
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		if(hiduserservice.addHiduser(hid, fid, maintainer, user.id)){
			if(declaresService.createWo(hid)){
				totalstateService.updatestate(hid, fid, 1);
			    return renderSuccess();
			}
		}
	    return renderError("添加失败");
	}
	/**
	 * 按类型查找
	 */
	@RequestMapping("/bystate")
	@ResponseBody
	public List<Declares> selectState(int state,int fid,int page){
		return declaresService.selectdeclareByState(fid, state, page);
	}
	
	@RequestMapping("/finishcount")
	@ResponseBody
	public int finishcount(int state){
		return declaresService.finishcounts(state);
	}
	
	@RequestMapping("/selectfinish")
	@ResponseBody
	public List<Declares> selectfinish(int state,int page){
		return declaresService.selectfinish(state, page);
	}
}













